Socket
Socket
Sign inDemoInstall

@formatjs/intl-utils

Package Overview
Dependencies
Maintainers
2
Versions
64
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@formatjs/intl-utils

Smartly determine best unit for relative time format


Version published
Weekly downloads
351K
increased by2.98%
Maintainers
2
Install size
139 kB
Created
Weekly downloads
 

Package description

What is @formatjs/intl-utils?

@formatjs/intl-utils is a utility library that provides various internationalization (i18n) utilities. It is part of the FormatJS suite and is designed to help with formatting dates, numbers, and messages in a way that is locale-aware.

What are @formatjs/intl-utils's main functionalities?

Date Formatting

This feature allows you to format dates according to the specified locale and options. The example formats the current date in the 'en-US' locale.

const { formatDate } = require('@formatjs/intl-utils');
const date = new Date();
const formattedDate = formatDate(date, { year: 'numeric', month: 'long', day: 'numeric' }, 'en-US');
console.log(formattedDate); // Output: 'October 5, 2023'

Number Formatting

This feature allows you to format numbers according to the specified locale and options. The example formats a number as a currency in the 'en-US' locale.

const { formatNumber } = require('@formatjs/intl-utils');
const number = 1234567.89;
const formattedNumber = formatNumber(number, { style: 'currency', currency: 'USD' }, 'en-US');
console.log(formattedNumber); // Output: '$1,234,567.89'

Message Formatting

This feature allows you to format messages with placeholders according to the specified locale and values. The example formats a message by replacing the placeholder with a value in the 'en-US' locale.

const { formatMessage } = require('@formatjs/intl-utils');
const message = 'Hello, {name}!';
const values = { name: 'John' };
const formattedMessage = formatMessage(message, values, 'en-US');
console.log(formattedMessage); // Output: 'Hello, John!'

Other packages similar to @formatjs/intl-utils

Readme

Source

Intl Utils

Provide i18n utilities.

npm Version

API

selectUnit

This function determines the best fit unit based on a specific set of customizable thresholds.

function selectUnit(
  from: Date | number,
  to: Date | number = Date.now(),
  thresholds = DEFAULT_THRESHOLDS
): {value: number; unit: Unit};

where thresholds has the shape of:

interface Threshold {
  second: number;
  minute: number;
  hour: number;
  day: number;
}

month & year are based on calendar, thus not customizable.

Example:

import {selectUnit} from '@formatjs/intl-utils';
selectUnit(Date.now() - 1000); // { value: -1, unit: 'second' }
selectUnit(Date.now() - 44000); // { value: -44, unit: 'second' }
selectUnit(Date.now() - 50000); // { value: 1, unit: 'minute' }
Caveats

selectUnit is meant to be a stepping stone from the old IntlRelativeFormat to the officially spec-ed Intl.RelativeTimeFormat. Therefore we don't recommend using this for an extended period of time because of ambiguous editorial issues such as:

  • From 2019/01/01 -> 2018/11/01 can technically be last year, 2 months ago or a quarter ago.

  • From 2019/01/02 6am to 2019/01/01 11pm can also be 7 hours ago or yesterday. Timezone further complicates the issue.

The examples above have not even tackled the differences in non-Gregorian calendars. There is an issue opened upstream in the spec that potentially introduces a best fit algorithm. Therefore, we recommend that you implement your own version of selectUnit that matches your editorial expectation.

Keywords

FAQs

Package last updated on 02 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc